package com.ghck.server.bkcontroller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ghck.server.exception.GhckException;
import com.ghck.server.exception.ErrorConstant;
import com.ghck.server.model.Knowledge;
import com.ghck.server.model.request.KnowledgeReq;
import com.ghck.server.model.response.Result;
import com.ghck.server.service.KnowledgeService;
import com.ghck.server.utils.FileStoreUtil;

/**
 * 疾病相关接口
 * @author chenxuan
 * 
 * t_disease
 * dict_bodypart
 * r_disease_bodypart
 */
@Controller
@RequestMapping(value="/admin/knowledge")
public class AdminKnowledgeController {
	
	Logger logger = LoggerFactory.getLogger(AdminKnowledgeController.class);

	@Autowired
	private KnowledgeService knowledgeService;
	
	/**
	 * 添加健康资讯
	 * @param knowledgeReq
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Result add(KnowledgeReq knowledgeReq){
		Knowledge knowledge = null;
		if (knowledge instanceof KnowledgeReq) {
			knowledge = knowledgeReq;
			
		}
		if(knowledgeReq.getImg() == null || knowledgeReq.getImg().isEmpty()){
			throw new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(), "请上传资讯配图,配置规格 640 * 240 ,不能大于512K");
		}
		if(knowledgeReq.getImg()!=null && knowledgeReq.getImg().getSize()>0){
			if (knowledgeReq.getImg().getSize() > 524288) {
				throw new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(), "资讯配图不能大于512K");
			}
			String filePath = FileStoreUtil.storeFile(knowledgeReq.getImg(), "knowledge");
			knowledge.setPicUrl(filePath);
		}
		knowledgeService.add(knowledge);
		return new Result();
	}
	
	/**
	 * 疾病分类列表
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.PUT)
	@ResponseBody
	public Result delete(Knowledge knowledge){
		knowledgeService.delete(knowledge);
		return new Result();
	}
	
}